package com.ljw.vendingmachine;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;

import com.ljw.vendingmachine.base.BaseActivity;
import com.ljw.vendingmachine.utils.Constant;
import com.ljw.vendingmachine.utils.SPUtils;

import butterknife.BindView;

public class OtherSettingActivity extends BaseActivity {
    @BindView(R.id.ed_number)
    EditText ed_number;
    @BindView(R.id.progress_volume)
    SeekBar progress_volume;
    @BindView(R.id.btn_Sava)
    Button btn_Sava;

    @Override
    public int setContentView() {
        return R.layout.activity_other_setting;
    }



    @Override
    public void initView() {
        if (SPUtils.contains(this, Constant.NUMBERKEY)){
            int number= (int) SPUtils.get(this,Constant.NUMBERKEY,1);
            ed_number.setText(String.valueOf(number));
        }
        btn_Sava.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trim = ed_number.getText().toString().trim();
                if (TextUtils.isEmpty(trim)){
                    Toast.makeText(OtherSettingActivity.this,"不能为空",Toast.LENGTH_LONG).show();
                    return;
                }
                try{
                    int anInt = Integer.parseInt(trim);
                    Constant.NUMBER=anInt;
                    SPUtils.put(OtherSettingActivity.this,Constant.NUMBERKEY, anInt);
                    Toast.makeText(OtherSettingActivity.this,"设置成功",Toast.LENGTH_LONG).show();
                    startActivity(new Intent(OtherSettingActivity.this,MainActivity.class));
                    finish();
                }catch (Exception e){
                    Toast.makeText(OtherSettingActivity.this,"请输入正整数",Toast.LENGTH_LONG).show();
                }
            }
        });

        AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
        //最大音量
        int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        progress_volume.setMax(maxVolume);
        progress_volume.setProgress(currentVolume);
        progress_volume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, AudioManager.FLAG_SHOW_UI);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    public void initData() {

    }


}
